var encryption = require('/encryption.js');
var common = require('/common.js');

var securityKey = "5F2030CB850D95E5EE4193E56F3FBAA5";//掌沃生活 app 验签key

function installmentsGoodsListByPage(paramenter, callback) {//月租商城商品列表
    var _url = common.baseWoyesurl + '/fqgClient/clientInterface?';

    // var _publicParas = common.getCommonParameter(_url);
    // _publicParas.method = "installmentsGoodsListByPage";
    // // _publicParas.pageNo = "1";
    // // _publicParas.pageSize = "10";
    // for (var key in paramenter) {
    //     console.log(key + " = " + paramenter[key]);
    //     _publicParas[key] = paramenter[key];
    // }
    // var sign = common.getSign(_publicParas);
    // _publicParas.sign = sign;
    if (paramenter == null) {
        paramenter = { method: "installmentsGoodsListByPage" };
    } else {
        paramenter.method = "installmentsGoodsListByPage";
    }
    baseRequest(_url, paramenter, callback)
}

function goodsDetail(paramenter, callback) { //月租商城商品详情
    var _url = common.baseWoyesurl + '/fqgClient/clientInterface?';
    if (paramenter == null) {
        paramenter = { method: "goodsDetail" };
    } else {
        paramenter.method = "goodsDetail";
    }
    baseRequest(_url, paramenter, callback)
}

function appOrderSubmitForClient(paramenter, callback) {//月租订单提交资料接口
    var _url = baseFqurl + '/fenqi/clientInterface?';
    if (paramenter == null) {
        paramenter = { method: "appOrderSubmitForClient" };
    } else {
        paramenter.method = "appOrderSubmitForClient";
    }
    baseRequest(_url, paramenter, callback);
}



function baseRequest(url, paramenter, callback) {

    if (null == url || '' == url.trim()) {
        console.log("请求链接不能为空!")
        return;
    }

    var _publicParas = common.getCommonParameter(url);
    if (paramenter != null) {
        for (var key in paramenter) {
            console.log(key + " = " + paramenter[key]);
            _publicParas[key] = paramenter[key];
        }
    }
    var sign = common.getSign(_publicParas);
    _publicParas.sign = sign;

    wx.request({
        url: url,
        data: _publicParas,
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        }, // 设置请求的 header
        success: function (res) {
            // success
            if (callback != null) {
                callback.success(res);
            }
        },
        fail: function () {
            // fail
            if (callback != null) {
                callback.fail();
            }
        },
        complete: function () {
            // complete
            if (callback != null) {
                callback.complete();
            }
        }
    })
}

// function mergeObj(o1, o2) {
//     var _temp = {};
//     var _paramenterStr = '';
//     for (var key in o1) {
//         _temp[key] = o1[key];
//     }
//     for (var key in o2) {
//         _temp[key] = o2[key];
//     }

//     for (var key in _temp) {
//         _paramenterStr += key + '=' + _temp[key] + '&';
//     }
//     _paramenterStr += encryption.MD5(securityKey);
//     _temp['sign'] = encryption.MD5(_paramenterStr);
//     console.log(_paramenterStr);
//     return _temp;
// }

module.exports = {
    installmentsGoodsListByPage: installmentsGoodsListByPage,
    goodsDetail:goodsDetail
}